package br.com.dufs.mvc.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Type;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import com.fscoreunipe.app.infra.annotations.CRIPT_MD5;
import com.fscoreunipe.app.infra.annotations.Create_AT;

@Create_AT
@Entity
@Table(name = "USUARIO")
public class Usuario implements java.io.Serializable, UserDetails {

		private static final long serialVersionUID = 1L;

		@Id
		@Column(name = "login", unique = true)
		private String login;

		@CRIPT_MD5
		@Column(name = "senha")
		private String senha;

		@Column
		private String nome;

		@Column(name = "ativo")
		@Type(type = "boolean")
		private boolean ativo = true;

		@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
		@JoinTable(name = "usuario_perfil", joinColumns = @JoinColumn(name = "id_usuario"), inverseJoinColumns = @JoinColumn(name = "id_perfil"))
		private List<Perfil> perfis = new ArrayList<Perfil>();

		@Transient
		public Collection<GrantedAuthority> getAuthorities() {
			List<GrantedAuthority> lista = new ArrayList<GrantedAuthority>();
			for (Perfil perfil : getPerfis()) {
				lista.add(new GrantedAuthorityImpl(perfil.getAuthority()));
			}
			return lista;
		}

		@Transient
		public String getPassword() {
			return this.senha;
		}

		@Transient
		public String getUsername() {
			return this.login;
		}

		@Transient
		public boolean isAccountNonExpired() {
			return this.ativo;
		}

		@Transient
		public boolean isAccountNonLocked() {
			return this.ativo;
		}

		@Transient
		public boolean isCredentialsNonExpired() {
			return this.ativo;
		}
		@Transient
		public boolean isEnabled() {
			return this.ativo;
		}

		public boolean getAtivo() {
			return ativo;
		}

		public void setAtivo(boolean ativo) {
			this.ativo = ativo;
		}

		public String getLogin() {
			return login;
		}

		public void setLogin(String login) {
			this.login = login;
		}

		public String getSenha() {
			return senha;
		}

		public void setSenha(String senha) {
			this.senha = senha;
		}

		public String getNome() {
			return nome;
		}

		public void setNome(String nome) {
			this.nome = nome;
		}

		public List<Perfil> getPerfis() {
			return perfis;
		}

		public void setPerfis(List<Perfil> perfis) {
			this.perfis = perfis;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((login == null) ? 0 : login.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Usuario other = (Usuario) obj;
			if (login == null) {
				if (other.login != null)
					return false;
			} else if (!login.equals(other.login))
				return false;
				return true;
			}

		@Override
		public String toString() {
			return "Usuario [login=" + login + "]";
		}

}
